package leetcode.weekly.week320;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;

import leetcode.helper.tree.TreeNode;

//Solution2Test
public class Solution2 {

	TreeSet<Integer> set;

	public List<List<Integer>> closestNodes(TreeNode root, List<Integer> queries) {
		set = new TreeSet<>();
		f(root);
		List<List<Integer>> ans = new ArrayList<>();
		for (int i = 0; i < queries.size(); i++) {
			int query = queries.get(i);
			Integer min = set.floor(query);
			Integer max = set.ceiling(query);
			ans.add(Arrays.asList(new Integer[] { min == null ? -1 : min, max == null ? -1 : max }));
		}
		return ans;
	}

	private void f(TreeNode root) {
		if (root == null) {
			return;
		}
		set.add(root.val);
		this.f(root.left);
		this.f(root.right);
	}

}
